<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
        :modal-class="my-modal"
        dialog-class="modal-dialog"
        @hidden 关闭窗体触发事件
    -->
    <b-modal ref="my-modal" :title="title" scrollable  @hidden="onHidden" no-close-on-backdrop >
        <div class="d-block text-center">
            <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <el-input placeholder="输入关键字进行过滤" style="width:200px" v-model="filterText"> </el-input>
                    </div>
                    <div class="form-group m-form__group row">     
                        <div class="col-md-8">                                
                            <el-tree 
                                class="filter-tree"
                                :data="dataList" 
                                title="部门" 
                                node-key="id"
                                show-checkbox
                                :check-strictly="true"
                                :default-expand-all="false" 
                                :expand-on-click-node="false"
                                ref="lcDepartTree"
                                :filter-node-method="filterNode"
                                :props="defaultProps">
                            </el-tree>
                        </div>   
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="doSelect">
                <i class="icon-xl fas fa-save"></i> 确认
            </button>
        </template>
    </b-modal>    
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";
import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
export default {
    data(){
        return {
            title:"部门选择器",
            id:"",       
            filterText:"",     
            dataList:[],
            defaultProps: {
                children: 'children',
                label: 'text'
            },
        }
    },
    components: {
    },
    watch: {
        filterText(val) {
            this.$refs['lcDepartTree'].filter(val);  
        }
    },
    mounted() {        
       
    },
    methods: {        
        showModal(data) {
            this.filterText = "";
            this.$refs['my-modal'].show();         
            this.initTree();   
            
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        onHidden(){//关闭窗体触发事件
            // this.$emit("change",null);
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.text.indexOf(value) !== -1;
        },
        initTree() {            
            let params = {  };
            apiUtil.query(process.env.VUE_APP_SYS_API + "/sysOrg/depart/tree", params).then(({ data }) => {
                if (undefined == data.data || null == data.data || "" == data.data) {
                } else {
                    var data = eval("(" + data.data + ")");
                    this.dataList = data;
                    return;                    
                }
            });
        },
        doSelect(){
            let data = this.$refs['lcDepartTree'].getCheckedNodes();
            if(data.length === 0){
                handleAlert("请选择", "warning", 3)
                return;
            }
            // 选择前提示
            this.$confirm("确认选择记录吗?", "提示", {type: "warning",}).then(() => {
                this.hideModal();//关闭窗体
                this.$emit("change", data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
            }).catch(()=>{});//注意这里，这里是重点！！！; 
        }
   }
}
</script>